//
//  HQYFoodTableModel.m
//  Go!
//
//  Created by rimi on 16/7/13.
//  Copyright © 2016年 黄秋燕. All rights reserved.
//

#import "HQYFoodTableModel.h"
#import "NetWorkManager.h"
@interface HQYFoodTableModel ()
@property (nonatomic, strong) NSMutableArray *dataSourceArray; /**< 数据源 */
@property (nonatomic, strong) NSMutableArray *detailDataSourceArray; /**< 数据源 */
@end

@implementation HQYFoodTableModel
#pragma mark - 一级页面数据
- (void)getList {
    // 改变self的弱引用
    __weak typeof(self) weakSelf = self;
    
    // 获取团单网络请求
    [NetWorkManager getWithURL:@"searchshops" urlParam:@{@"city_id":[[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID], @"cat_ids":@"326"} successBlock:^(id responseObject) {
        weakSelf.response = responseObject[@"data"][@"shops"];
    } FailureBlock:nil];
}

- (NSArray *)getDataArray {
    NSArray *array = self.response;
    for (NSDictionary *obj in array) {
        NSMutableDictionary *dataSourceDic = [NSMutableDictionary dictionary];
        NSArray *shop = obj[@"deals"];
        NSMutableArray *shopArray = [NSMutableArray array];
        for (int i = 0; i < shop.count; i++) {
            NSString *foodId = shop[i][@"deal_id"];
            NSString *name = shop[i][@"min_title"];
            NSString *icon = shop[i][@"tiny_image"];
            NSString *price = [NSString stringWithFormat:@"%.2f",
                                   ([shop[i][@"current_price"] floatValue] / 100)];
            NSString *sellNum = [NSString stringWithFormat:@"%@",shop[i][@"sale_num"]];
            NSString *url = shop[i][@"deal_murl"];
            NSDictionary *deals = @{@"name":name, @"foodId":foodId, @"icon":icon, @"price":price, @"sellNum":sellNum, @"url":url};
            [shopArray addObject:deals];
        }
        [dataSourceDic setObject:shopArray forKey:@"shops"];
        [dataSourceDic setObject:obj[@"shop_id"] forKey:@"shopId"];
        [dataSourceDic setObject:obj[@"shop_name"] forKey:@"shopName"];
      
        [self.dataSourceArray addObject:dataSourceDic];
    }
    return [self.dataSourceArray copy];
}

- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

#pragma mark - 二级页面数据
- (void)getListWithClassID:(NSString *) classId
                    FoodId:(NSString *)foodId {
    // 改变self的弱引用
    __weak typeof(self) weakSelf = self;
    
    // 获取团单网络请求
    [NetWorkManager getWithURL:@"searchshops" urlParam:@{@"city_id":[[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID], @"cat_ids":classId, @"subcat_ids":foodId} successBlock:^(id responseObject) {
        weakSelf.responseDetail = responseObject[@"data"][@"shops"];
    } FailureBlock:nil];
}

- (NSArray *)getDetailDataArray {
    _detailDataSourceArray = [NSMutableArray array];
    NSArray *array = self.responseDetail;
    for (NSDictionary *obj in array) {
        NSMutableDictionary *dataSourceDic = [NSMutableDictionary dictionary];
        NSArray *shop = obj[@"deals"];
        NSMutableArray *shopArray = [NSMutableArray array];
        for (int i = 0; i < shop.count; i++) {
            NSString *foodId = shop[i][@"deal_id"];
            NSString *name = shop[i][@"min_title"];
            NSString *icon = shop[i][@"tiny_image"];
            NSString *price = [NSString stringWithFormat:@"%.2f",
                               ([shop[i][@"current_price"] floatValue] / 100)];
            NSString *sellNum = [NSString stringWithFormat:@"%@",shop[i][@"sale_num"]];
            NSString *url = shop[i][@"deal_murl"];
            NSDictionary *deals = @{@"name":name, @"foodId":foodId, @"icon":icon, @"price":price, @"sellNum":sellNum, @"url":url};
            [shopArray addObject:deals];
        }
        [dataSourceDic setObject:shopArray forKey:@"shops"];
        [dataSourceDic setObject:obj[@"shop_id"] forKey:@"shopId"];
        [dataSourceDic setObject:obj[@"shop_name"] forKey:@"shopName"];
        //        [dataSourceDic setObject:obj[@"longitude"] forKey:@"longitude"];
        //        [dataSourceDic setObject:obj[@"latitude"] forKey:@"latitude"];
        
        [self.detailDataSourceArray addObject:dataSourceDic];
    }
    return [self.detailDataSourceArray copy];
}

@end
